'Potential Pain and Distress - Requirement Script  
Option Strict Off
imports System
imports System.Collections
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
	sub Main()

		Dim litRefineqA as FormsManagement.QuestionnaireAnswer = nothing
		Dim litAltqA as FormsManagement.QuestionnaireAnswer = nothing
		
        for each qA as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
			if (qA.Name = "Literature Search- Refine") then
                litRefineqA = qA
			else if (qA.Name = "Literature Search- Alternatives") then
                litAltqA = qA
			end if
		next

        Dim answerNameValuePairs as new ArrayList
        Dim formAdded as boolean
        answerNameValuePairs.Add( "CatD" )
        answerNameValuePairs.Add( "Y" )
        answerNameValuePairs.Add( "CatE" )
        answerNameValuePairs.Add( "Y" )
        AddForm( answerNameValuePairs, "Literature Search- Refine", false, formAdded )

        if formAdded = false then
            answerNameValuePairs.Clear
            answerNameValuePairs.Add( "CatB" )
            answerNameValuePairs.Add( "Y" )
            answerNameValuePairs.Add( "CatC" )
            answerNameValuePairs.Add( "Y" )
            AddForm( answerNameValuePairs, "Literature Search- Alternatives", false, formAdded )
        else
            currentProtVersion.DeleteQuestionnaireAnswer("Literature Search- Alternatives")
        end if

		theProtocol.Save( connectionString )
		currentProtVersion.Save ( connectionString )
		currentProcess.Save( connectionString )
	end sub  
	
    ' Adds or removes questionnaire answers to protocol version based on answer to a question
    ' This overload takes mutliple questions / answers to determine if the QA needs to be added or 
    ' removed.
    sub AddForm(byval answerNameValuePairs as ArrayList, byval questionnaireName as string, byval formExists as boolean, byref formAdded as boolean)
        Dim add as boolean = false
        Dim i as integer
        for i = 0 to answerNameValuePairs.Count - 1 step 2
            Dim answers as Arraylist = questAnswer.GetAnswers( answerNameValuePairs(i) )
            if answers.Count > 0 then
                if answers(0).Value = answerNameValuePairs(i+1) then
                    add = true
                end if
            end if
        next
        if add and not formExists then
            Dim qv as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, questionnaireName, FormsManagement.Constants.QuestionnaireType.Animals )
            Dim qa as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, questionnaireName, not qv.Questionnaire.AllowMultiple)
            formAdded = true
        else if not add then
            currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
        end if
    end sub
end module
